package com.fuqiang.nightshopadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuqiang.nightshopadmin.mapper.IUmsRoleMapper;
import com.fuqiang.nightshopadmin.mapper.IUmsRoleMenuRelationMapper;
import com.fuqiang.nightshopadmin.mapper.IUmsRoleResourceRelationMapper;
import com.fuqiang.nightshopadmin.service.IUmsRoleService;
import com.fuqiang.nightshopumsmodel.entity.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author fuqiang
 * @since 2020-04-14
 */
@Service
@AllArgsConstructor
public class UmsRoleServiceImpl extends ServiceImpl<IUmsRoleMapper, UmsRole> implements IUmsRoleService {

    private IUmsRoleMapper roleMapper;
    private IUmsRoleMenuRelationMapper umsRoleMenuRelationMapper;
    private IUmsRoleResourceRelationMapper umsRoleResourceRelationMapper;
    @Override
    public List<UmsMenu> getMenuList(Long id) {
        return roleMapper.getMenuList(id);
    }

    @Override
    public IPage<UmsRole> list(String keyword, Integer pageSize, Integer pageNum) {
        QueryWrapper<UmsRole> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)){
            queryWrapper.lambda().like(UmsRole::getName,keyword);
        }

        Page<UmsRole> page = new Page<>(pageNum,pageSize);
        return roleMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<UmsMenu> listMenu(Long roleId) {
        return roleMapper.getMenuListByRoleId(roleId);
    }

    @Override
    public List<UmsResource> listResource(Long roleId) {
        return null;
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        QueryWrapper<UmsRoleMenuRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UmsRoleMenuRelation::getRoleId,roleId);
        umsRoleMenuRelationMapper.delete(queryWrapper);
        //批量插入新关系
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            umsRoleMenuRelationMapper.insert(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        // 删除原有资源信息
        QueryWrapper<UmsRoleResourceRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UmsRoleResourceRelation::getRoleId,roleId);
        umsRoleResourceRelationMapper.delete(queryWrapper);
        // 保存现有资源
        resourceIds.forEach(resourceId -> {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            umsRoleResourceRelationMapper.insert(relation);
        });
        return resourceIds.size();
    }
}
